package cn.cjxy;

import cn.cjxy.domain.User;
import cn.cjxy.mapper.UserMapper;
import cn.cjxy.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * 测试类
 */
@SpringBootTest(classes = Spring_MPApp.class)//启动类的字节码文件
@RunWith(SpringRunner.class)
public class Demo3 {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;
    //无条件分页查询
    //IPage<T> page(IPage<T> page);
    @Test
    public void test1() {
        //分装分页信息
        Page<User> userPage = new Page<>(2,3);//在第一页，每一页三条
        // 根据 entity 条件，查询全部记录（并翻页）
        //IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);//执行分页查询
        Page<User> userPage1 = userMapper.selectPage(userPage, null);
        //获取查询记录----当前页数据
        List<User> records = userPage1.getRecords();
        //获取条数
        System.out.println(userPage1.getTotal());
    }
    //带条件分页查询
    @Test
    public void test2() {
        //封装查询条件
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name","o");
        //封装分页信息
        Page<User> objectPage = new Page<>(1, 5);
        //执行查询
        userMapper.selectPage(objectPage, userQueryWrapper);
        objectPage.getRecords().forEach(System.out::println);
    }


    //service层  除了方法名不一样，其他一致


}
